﻿using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Windows.Forms;
using EzPos.Model.Common;
using EzPos.Model.SaleOrder;
using EzPos.Service.Common;
using Font = System.Drawing.Font;
using Point = System.Drawing.Point;
using Rectangle = System.Drawing.Rectangle;
using ExcelApplication = Microsoft.Office.Interop.Excel.Application;

namespace EzPos.GUIs.Forms
{
    public class PrintReceipt
    {
        private readonly PrintDocument _printDocument = new PrintDocument();
        private IList _saleItemList;
        private string _cashier;
        private string _tableName;
        private float _discount;
        private string _invoiceNumber;
        private string _printDate;
        private float _exchangeRate;
        internal PrintPreviewDialog PrintPreviewDialog;
        private float _invoiceFontSize;
        private string _invoiceFontName;

        public float Discount
        {
            set { _discount = value; }
        }

        public string TableName
        {
            set { _tableName = value; }
        }

        public IList SaleItemList
        {
            set { _saleItemList = value; }
        }

        public string InvoiceNumber
        {
            set { _invoiceNumber = value; }
        }

        public float ExchangeRate
        {
            set { _exchangeRate = value; }
        }

        public string Cashier
        {
            set { _cashier = value; }
        }

        public string PrintDate
        {
            set { _printDate = value; }
        }

        public void InializeReceiptPrinting()
        {
            PrintPreviewDialog =
                new PrintPreviewDialog
                {
                    UseAntiAlias = true
                };

            _invoiceFontSize = AppContext.InvoiceFontSize;
            _invoiceFontName = AppContext.InvoiceFontName;

            _printDocument.PrinterSettings.PrinterName = AppContext.ReceiptPrinter;
            //_printDocument.PrinterSettings.PrinterName = "ReceiptPrinter";
            _printDocument.DocumentName = AppContext.ShopName;            
            _printDocument.PrintPage += PrintDocumentPrintPage;
            PrintPreviewDialog.Document = _printDocument;
            _printDocument.Print();
            //PrintPreviewDialog.Size = new Size(1024, 768);
            //PrintPreviewDialog.ShowDialog();
            _printDocument.PrintPage -= PrintDocumentPrintPage;
        }

        private void PrintDocumentPrintPage(object sender, PrintPageEventArgs e)
        {
            var posY = 0;

            var logoFile = CommonService.RetrieveApplicationLogo();
            if (logoFile != null)
            {
                var bitmap = logoFile as Bitmap;
                if (bitmap != null)
                    e.Graphics.DrawImage(bitmap, 100f, 0f, 91f, 81f);
            }

            var textToPrint = AppContext.ShopName;
            var fontInUsed = new Font("Khmer OS Muol Light", 25, FontStyle.Regular);            

            posY += 150;
            TextRenderer.DrawText(
                            e.Graphics,
                            textToPrint,
                            fontInUsed,
                            new Rectangle(0, posY, 515, 80),
                            SystemColors.WindowText,
                            TextFormatFlags.HorizontalCenter);

            fontInUsed = new Font(_invoiceFontName, _invoiceFontSize, FontStyle.Regular);             
            posY += 50;
            textToPrint = AppContext.ShopAddress;
            TextRenderer.DrawText(
                            e.Graphics,
                            textToPrint,
                            fontInUsed,
                            new Rectangle(0, posY, 515, 80),
                            SystemColors.WindowText,
                            TextFormatFlags.HorizontalCenter);

            posY += 40;
            textToPrint = AppContext.ShopContact;
            TextRenderer.DrawText(
                            e.Graphics,
                            textToPrint,
                            fontInUsed,
                            new Rectangle(0, posY, 515, 80),
                            SystemColors.WindowText,
                            TextFormatFlags.HorizontalCenter);

            var pen = new Pen(Color.Black, 0.1f);
            e.Graphics.DrawLine(pen, -10, posY - 75, 320, posY - 75);
            pen = new Pen(Color.Black, 2f);
            e.Graphics.DrawLine(pen, 0, posY - 78, 320, posY - 78);

            posY += 70;
            textToPrint = "លេខ​តុ: " + _tableName; 
            TextRenderer.DrawText(
                e.Graphics,
                textToPrint,
                fontInUsed,
                new Point(-5, posY),
                SystemColors.WindowText,
                Color.Transparent,
                TextFormatFlags.Default);

            //textToPrint = "វិក្កយប័ត្រ: " + _invoiceNumber;            
            //TextRenderer.DrawText(
            //                e.Graphics,
            //                textToPrint,
            //                fontInUsed,
            //                new Rectangle(295, posY, 220, 40),
            //                SystemColors.WindowText,
            //                TextFormatFlags.Right);

            posY += 40;
            textToPrint = "កាលបរិច្ឆេទ: " + _printDate;
            TextRenderer.DrawText(
                e.Graphics,
                textToPrint,
                fontInUsed,
                new Point(-5, posY),
                SystemColors.WindowText,
                Color.Transparent,
                TextFormatFlags.Default);

            textToPrint = "បេឡាករ: " + _cashier;
            TextRenderer.DrawText(
                e.Graphics,
                textToPrint,
                fontInUsed,
                new Rectangle(295, posY, 220, 40),
                SystemColors.WindowText,
                TextFormatFlags.Right);

            pen = new Pen(Color.Black, 0.1f);
            var rectangle = new Rectangle(0, 222, 283, 30);
            e.Graphics.DrawRectangle(pen, rectangle);
            //e.Graphics.FillRectangle(pen.Brush, rectangle);

            posY += 57;            
            textToPrint = "បរិយាយ                                  ចំនួន      តំលៃ       សរុប";
            TextRenderer.DrawText(
                e.Graphics,
                textToPrint,
                fontInUsed,
                new Point(-3, posY),
                SystemColors.WindowText,
                Color.White,
                TextFormatFlags.Default);

            posY += 60;
            //var sizeY = posY - 105;
            foreach (var saleItem in _saleItemList.Cast<SaleItem>().Where(saleItem => saleItem.ProductId != 0))
            {
                //Name                
                textToPrint = saleItem.FkProduct.Description;
                if (string.IsNullOrEmpty(textToPrint))
                {
                    textToPrint = saleItem.ProductName;
                    fontInUsed = new Font("Arial", 8, FontStyle.Regular);
                    e.Graphics.DrawString(textToPrint, fontInUsed, Brushes.Black, 0, posY);
                }   
                else
                {
                    TextRenderer.DrawText(
                        e.Graphics,
                        textToPrint,
                        fontInUsed,
                        new Point(-5, posY),
                        SystemColors.WindowText,
                        Color.Transparent,
                        TextFormatFlags.Default);                    
                }

                //Qty
                textToPrint = saleItem.QtySold.ToString("N0", AppContext.CultureInfo);
                TextRenderer.DrawText(
                    e.Graphics,
                    textToPrint,
                    fontInUsed,
                    new Rectangle(250, posY, 100, 35),
                    SystemColors.WindowText,
                    TextFormatFlags.Right);

                //UnitPrice
                textToPrint = Math.Round(saleItem.UnitPriceOut, 2).ToString("N0", AppContext.CultureInfo);
                TextRenderer.DrawText(
                    e.Graphics,
                    textToPrint,
                    fontInUsed,
                    new Rectangle(325, posY, 100, 35),
                    SystemColors.WindowText,
                    TextFormatFlags.Right);

                //SubTotal
                var subTotalAmountLocal = saleItem.QtySold*saleItem.UnitPriceOut;
                //totalAmountLocal += subTotalAmountLocal;
                textToPrint = Math.Round(subTotalAmountLocal, 2).ToString("N0", AppContext.CultureInfo);
                TextRenderer.DrawText(
                    e.Graphics,
                    textToPrint,
                    fontInUsed,
                    new Rectangle(395, posY, 120, 35),
                    SystemColors.WindowText,
                    TextFormatFlags.Right);

                posY += 40;
            }

            //Amount
            var discountedTotalAmount =
                (from saleItem
                 in _saleItemList.Cast<SaleItem>()
                 where
                    (saleItem.QtyBonus == 0)
                    && (saleItem.FkProduct != null)
                    && ((from categoryId in AppContext.DiscountProductCategoryList select categoryId).Contains(saleItem.FkProduct.CategoryId))
                 select (saleItem.QtySold * saleItem.UnitPriceOut)).Sum();

            var nonDiscountedTotalAmount =
                (from saleItem
                in _saleItemList.Cast<SaleItem>()
                 where
                     (saleItem.FkProduct != null)
                     && ((saleItem.QtyBonus != 0)
                     || !((from categoryId in AppContext.DiscountProductCategoryList select categoryId).Contains(saleItem.FkProduct.CategoryId)))
                 select (saleItem.QtySold * saleItem.UnitPriceOut)).Sum();

            //discountedTotalAmount = discountedTotalAmount - ((discountedTotalAmount * _discount) / 100);
            var discountAmountLocal = (discountedTotalAmount * _discount) / 100;
            var discountAmountForeign = discountAmountLocal / _exchangeRate;

            //Total amount
            var totalAmountLocal = discountedTotalAmount + nonDiscountedTotalAmount;
            var totalAmountForeign = totalAmountLocal / _exchangeRate;

            var amountToPayLocal = totalAmountLocal - discountAmountLocal;
            var amountToPayForeign = totalAmountForeign - discountAmountForeign;

            posY += 30;
            var subPosY = Int32.Parse(Math.Round(posY / 1.77, 0).ToString());
            e.Graphics.DrawLine(pen, 0, subPosY, 320, subPosY);

            posY += 30;
            fontInUsed = new Font(AppContext.InvoiceFontName, AppContext.InvoiceFontSize, FontStyle.Bold);
            textToPrint = "សរុប:";
            TextRenderer.DrawText(
                e.Graphics,
                textToPrint,
                fontInUsed,
                new Point(150, posY),
                SystemColors.WindowText,
                Color.Transparent,
                TextFormatFlags.Default);
            
            textToPrint = totalAmountLocal.ToString("N0", AppContext.CultureInfo) + " ៛";
            TextRenderer.DrawText(
                e.Graphics,
                textToPrint,
                fontInUsed,
                new Rectangle(300, posY, 125, 35),
                SystemColors.WindowText,
                TextFormatFlags.Right);

            textToPrint = "$ " + totalAmountForeign.ToString("N2", AppContext.CultureInfo);
            TextRenderer.DrawText(
                e.Graphics,
                textToPrint,
                fontInUsed,
                new Rectangle(395, posY, 120, 35),
                SystemColors.WindowText,
                Color.Transparent,
                TextFormatFlags.Right);

            posY += 40;
            textToPrint = "បញ្ចុះ​តំលៃ(" + _discount.ToString("N0", AppContext.CultureInfo) +"%)*:";
            TextRenderer.DrawText(
                e.Graphics,
                textToPrint,
                fontInUsed,
                new Point(150, posY),
                SystemColors.WindowText,
                Color.Transparent,
                TextFormatFlags.Default);

            textToPrint = discountAmountLocal.ToString("N0", AppContext.CultureInfo) + " ៛";
            TextRenderer.DrawText(
                e.Graphics,
                textToPrint,
                fontInUsed,
                new Rectangle(300, posY, 125, 35),
                SystemColors.WindowText,
                TextFormatFlags.Right);

            textToPrint = "$ " + discountAmountForeign.ToString("N2", AppContext.CultureInfo);
            TextRenderer.DrawText(
                e.Graphics,
                textToPrint,
                fontInUsed,
                new Rectangle(395, posY, 120, 35),
                SystemColors.WindowText,
                Color.Transparent,
                TextFormatFlags.Right);

            posY += 40;
            textToPrint = "ប្រាក់​ត្រូវបង់:";
            TextRenderer.DrawText(
                e.Graphics,
                textToPrint,
                fontInUsed,
                new Point(150, posY),
                SystemColors.WindowText,
                Color.Transparent,
                TextFormatFlags.Default);

            textToPrint = amountToPayLocal.ToString("N0", AppContext.CultureInfo) + " ៛";
            TextRenderer.DrawText(
                e.Graphics,
                textToPrint,
                fontInUsed,
                new Rectangle(300, posY, 125, 35),
                SystemColors.WindowText,
                TextFormatFlags.Right);

            textToPrint = "$ " + amountToPayForeign.ToString("N2", AppContext.CultureInfo);
            TextRenderer.DrawText(
                e.Graphics,
                textToPrint,
                fontInUsed,
                new Rectangle(395, posY, 120, 35),
                SystemColors.WindowText,
                Color.Transparent,
                TextFormatFlags.Right);
            
            //posY += 80;
            fontInUsed = new Font(_invoiceFontName, _invoiceFontSize, FontStyle.Regular);             
            //textToPrint = "* ការ​បញ្ចុះ​តំលៃ​ត្រូវ​បាន​លើក​លែង​លើ​ភេសជ្ជៈ";
            //TextRenderer.DrawText(
            //    e.Graphics,
            //    textToPrint,
            //    fontInUsed,
            //    new Rectangle(0, posY, 515, 50),
            //    SystemColors.WindowText,
            //    Color.Transparent,
            //    TextFormatFlags.HorizontalCenter);

            //posY += 40;
            //textToPrint = "* Discount is not applicable for beverage";
            //TextRenderer.DrawText(
            //    e.Graphics,
            //    textToPrint,
            //    fontInUsed,
            //    new Rectangle(0, posY, 515, 35),
            //    SystemColors.WindowText,
            //    Color.Transparent,
            //    TextFormatFlags.HorizontalCenter);

            posY += 50;
            subPosY = Int32.Parse(Math.Round(posY/1.77, 0).ToString());            
            e.Graphics.DrawLine(pen, 0, subPosY - 5, 320, subPosY - 5);
            pen = new Pen(Color.Black, 2f);
            e.Graphics.DrawLine(pen, 0, subPosY - 2, 320, subPosY - 2);

            posY += 20;
            textToPrint = AppContext.ReceiptFooter;
            TextRenderer.DrawText(
                e.Graphics,
                textToPrint,
                fontInUsed,
                new Rectangle(0, posY, 500, 35),
                SystemColors.WindowText,
                Color.Transparent,
                TextFormatFlags.HorizontalCenter);

            posY += 30;
            textToPrint = "Powered by MatrixBox";
            TextRenderer.DrawText(
                            e.Graphics,
                            textToPrint,
                            fontInUsed,
                            new Rectangle(0, posY, 500, 35),
                            SystemColors.WindowText,
                            Color.Transparent,
                            TextFormatFlags.HorizontalCenter);
        }        
    }
}